﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;



namespace StudentManage
{
    public partial class MH_Import_diem : Form
    {

       
        private static Microsoft.Office.Interop.Excel.Application appExcel;
        private static Workbook newWorkBook = null;
        private static _Worksheet objSheet = null;

        static void excel_init(String path)
        {
            appExcel = new Microsoft.Office.Interop.Excel.Application();
            
            if (System.IO.File.Exists(path))
            {
                newWorkBook = appExcel.Workbooks.Open(path, true, true);
                objSheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
            }
            else
            {
                MessageBox.Show("Unable to open file!");
                System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
                appExcel = null;
                System.Windows.Forms.Application.Exit();
            }
        }
        static string excel_getValue(string cellname)
        {
            string value = string.Empty;
            try
            {
                value =objSheet.get_Range(cellname).get_Value().ToString();
            }
            catch
            {
                value = "";
            }

            return value;
        }

        //Method to close excel connection
        static void excel_close()
        {
            if (appExcel != null)
            {
                try
                {
                    newWorkBook.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
                    appExcel = null;
                    objSheet = null;
                }
                catch (Exception ex)
                {
                    appExcel = null;
                    MessageBox.Show("Unable to release the Object " + ex.ToString());
                }
                finally
                {
                    GC.Collect();
                }
            }
        }
        public MH_Import_diem()
        {
            InitializeComponent();

            excel_init("C:\\Users\\Welcome\\Documents\\1.xlsx");

            MessageBox.Show("OK");
        }
    }
}
